﻿namespace KinonekoSoftware.Studio.Pages.Theories
{
    public class VocabularyEditorViewModel : RepositoryGalleryItem
    {
        private readonly IRepository   _repository;
        private readonly TheoryEngine_Vocabulary _theory;
        
        public VocabularyEditorViewModel()
        {
            _repository = Platform.GetRepository();
            _theory     = Platform.GetEngine<TheoryEngine_Vocabulary>();
            
            
            Vocabulary = new ObservableCollection<Vocabulary>();

            NewCommand           = CreateCommand(DoNewCommand);
            EditCommand          = CreateCommand<Vocabulary>(DoEditCommand);
            EditTemplatedCommand = CreateCommand<Vocabulary>(DoEditTemplatedCommand);
            OpenNotionCommand    = CreateCommand<Vocabulary>(DoOpenNotionCommand);
            OpenStoryCommand     = CreateCommand<Vocabulary>(DoOpenStoryCommand);
            RemoveCommand        = CreateCommand<Vocabulary>(DoRemoveCommand);


            Vocabulary.AddMany(_theory.Collection);
        }

        private void Save()
        {
            Xplat.Debug($"正在保存TheoryOfVocabularies属性，Version:{_theory.Version}");
        }
        
        
        //-------------------------------------------------
        //
        //          Command Handlers
        //
        //-------------------------------------------------

        private async void DoNewCommand()
        {
            var r = await NewVocabularyViewModel.Create();

            if (!r.IsFinished)
            {
                return;
            }

            var val = r.Value;

            //
            //
            _theory.Add(val);

            //
            //
            Vocabulary.Add(val);
            
            //
            Save();
        }

        private void DoEditTemplatedCommand(Vocabulary element)
        {
            if (element is null)
            {
                return;
            }

            // var context = new PageContext
            // {
            //     Id = element.Id
            // };
            // context.Set(ContextBase.Key_Value, element);
            // Core.GoTo<TemplatedEditorViewVocabulary>(context);
            
            //
            Save();
        }

        private async void DoEditCommand(Vocabulary element)
        {
            if (element is null)
            {
                return;
            }

            var r = await NewVocabularyViewModel.Edit(element);

            if (!r.IsFinished)
            {
                return;
            }

            //
            //
            Save();
        }
        
        
        private void DoOpenNotionCommand(Vocabulary element)
        {
            this.ThisNotion(element);
        }


        private void DoOpenStoryCommand(Vocabulary element)
        {
            this.ThisStory(element);
        }


        private async void DoRemoveCommand(Vocabulary element)
        {
            if (element is null)
            {
                return;
            }

            if (!await RemoveItem())
            {
                return;
            }

            Vocabulary.Remove(element);
            _theory.Remove(element);
            
            //
            //
            Save();
        }
        
        
        public ICommandEX NewCommand           { get; }
        public ICommandEX EditCommand          { get; }
        public ICommandEX EditTemplatedCommand { get; }
        public ICommandEX OpenNotionCommand    { get; }
        public ICommandEX OpenStoryCommand     { get; }
        public ICommandEX RemoveCommand        { get; }

        public ObservableCollection<Vocabulary> Vocabulary { get; }
    }
}